<?php
session_start();
if (isset($_SESSION["username"])) {
?>
  <?php require_once('common/header.php'); ?>
  <!--页面主要内容-->
  <main class="lyear-layout-content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-toolbar clearfix">
              <form class="pull-right search-bar" method="get" action="" role="form">
                <div class="input-group">
                  <div class="input-group-btn">
                    <input type="hidden" name="search" id="search" value="proxy_acc">
                    <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                      代理账号 <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="proxy_acc">代理账号</a> </li>
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="proxy_phone">代理手机</a> </li>
                    </ul>
                  </div>
                  <input type="text" class="form-control" value="" name="keyword" placeholder="请输入搜索信息">
                </div>
              </form>
              <div class="toolbar-btn-action">
                  <!--弹窗开始-->
         
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo"><i class="mdi mdi-plus"></i>添加代理</button>
                <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                  <div class="modal-dialog" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="exampleModalLabel">添加代理</h4>
                      </div>
                      <div class="modal-body">
                          
                          <!--查询代理组-->
                          <?php
                                include_once("../untils/conn.php");
                                mysqli_query($con, "set names utf8");
                                $sql = "SELECT group_id, group_name FROM proxy_group";
                                $result = mysqli_query($con, $sql);
                            ?>
                          
                          
                        <form action="controller/Doinproxy.php" method="post">
                          <div class="form-group">
                            <label for="proxy_acc" class="control-label">登录账号：</label>
                            <input type="text" class="form-control" id="proxy_acc" name="proxy_acc">
                          </div>
                          <div class="form-group">
                            <label for="proxy_password" class="control-label">登录密码：</label>
                            <input type="text" class="form-control" id="proxy_password" name="proxy_password">
                          </div>
                          <div class="form-group">
                            <label for="proxy_phone" class="control-label">手机号码：</label>
                            <input type="text" class="form-control" id="proxy_phone" name="proxy_phone">
                          </div>
                          <div class="form-group">
                            <label for="proxy_name" class="control-label">代理昵称：</label>
                            <input type="text" class="form-control" id="proxy_name" name="proxy_name">
                          </div>
                          <div class="form-group">
                            <label for="proxy_rename" class="control-label">代理真实姓名：</label>
                            <input type="text" class="form-control" id="proxy_rename" name="proxy_rename">
                          </div>
                          <div class="form-group">
                            <label for="proxy_uid" class="control-label">代理身份证号：</label>
                            <input type="text" class="form-control" id="proxy_uid" name="proxy_uid">
                          </div>
                          <div class="form-group">
                            <label for="proxy_type">代理类型</label>
                            <div >
                              <select class="form-control" id="proxy_type" name="proxy_type" size="1">
                                <option value="0">个人</option>
                                <option value="1">个体/企业</option>
                              </select>
                            </div>
                          </div>
                          <div class="form-group">
                                <label for="group_id">代理等级</label>
                                <select class="form-control" id="group_id" name="group_id" size="1">
                                    <?php while($row = mysqli_fetch_assoc($result)): ?>
                                        <option value="<?php echo $row['group_id']; ?>"><?php echo $row['group_name']; ?></option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                          <!--时间戳-->
                          <input type="hidden" name="cr_time" value="<?php echo time(); ?>">

                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">提交</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                      </div>
                        </form>
                    </div>
                  </div>
                </div>
                <!--这是弹窗结束-->

              </div>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <form name="form1" id="form1" method="post" action="controller/deleteall.php">
                  <table class="table table-bordered">
                <?php
                include_once("../untils/conn.php");
                mysqli_query($con, "set names utf8");
                if ($con) {
                  if ($db) {
                    // 获取搜索关键词和字段
                    $keyword = $_GET['keyword'];
                    $field = $_GET['search'];
                
                    // 构建查询语句
                    $sql = "SELECT * FROM proxy";
                    if ($keyword != "") {
                      $sql .= " WHERE $field LIKE '%$keyword%'";
                    }
                
                    // 获取数据总行数
                    $data = mysqli_query($con, $sql);
                    $maxrows = mysqli_num_rows($data);
                
                    // 计算总页数
                    $page_size = 10; // 每页显示数
                    if ($maxrows % $page_size == 0) {
                      $maxpage = (int)($maxrows / $page_size);
                    } else {
                      $maxpage = (int)($maxrows / $page_size) + 1;
                    }
                
                    // 获取当前页
                    if (isset($_GET['curpage'])) {
                      $page = $_GET['curpage'];
                    } else {
                      $page = 1;
                    }
                
                    // 分段取出数据
                    $start = $page_size * ($page - 1);
                    $get_sql = "$sql ORDER BY proxy_id DESC LIMIT $start, $page_size";
                
                    // 获取数据显示
                    $data = mysqli_query($con, $get_sql);
                ?>


                    <?php 
                    $proxy_upid = $row["proxy_upid"]
                    
                    ?>
                        <thead>
                          <tr>
                            <th>代理ID</th>
                            <th>代理账号</th>
                            <th>手机号</th>
                            <th>名称</th>
                            <th>余额</th>
                            <th>实名</th>
                            <th>身份证号</th>
                            <th>上级代理</th>
                            <th>支付宝</th>
                            <th>支付宝账号</th>
                            
                            <th>代理等级</th>
                            <th>账号状态</th>
                            <th>封禁原因</th>
                            <th>实名状态</th>
                            <th>类型</th>
                            <th>创建时间</th>
                            
                            <th>操作</th>
                          </tr>
                        </thead>
                        <tbody>
                        <?php
                        $proxy_id = null; // 定义变量
                        while ($row = mysqli_fetch_array($data)) {
                            // 获取上级代理名称
                            $proxy_upid = $row["proxy_upid"];
                            if ($proxy_upid != "") {
                                $get_up_proxy_name_sql = "SELECT proxy_name FROM proxy WHERE proxy_id = $proxy_upid";
                                $get_up_proxy_name_result = mysqli_query($con, $get_up_proxy_name_sql);
                                $up_proxy_name = mysqli_fetch_array($get_up_proxy_name_result)["proxy_name"];
                            } else {
                                $up_proxy_name = "直属平台";
                            }
                        ?>
                            <tr>
                                <td style="width:2%;">
                                    <?php echo  $proxy_id = $row["proxy_id"] ?>
                                </td>
                                <td style="width:3%;">
                                    <?php echo $row["proxy_acc"] ?>
                                </td>
                                <td style="width:3%;">
                                    <?php echo $row["proxy_phone"] ?>
                                </td>
                                <td style="width:7.5%;">
                                    <?php echo $row["proxy_name"] ?>
                                </td>
                                 <td style="width:5%;">
                                    ￥<?php echo $row["proxy_balance"] ?>
                                </td>
                                <td style="width:5%;">
                                    <?php echo $row["proxy_rename"] ?>
                                </td>
                                <td style="width:5%;">
                                    <?php echo $row["proxy_uid"] ?>
                                </td>
                                <td style="width:6.5%;">
                                    <?php echo $up_proxy_name ?>
                                </td>
                                <td style="width:5%;">
                                    <?php echo $row["proxy_alipay"] ?>
                                </td>
                                <td style="width:5%;">
                                    <?php echo $row["proxy_alipay_acc"] ?>
                                </td>
                                
                                <td style="width:7%;">
                                    <?php 
                                         echo $row["group_id"];
                                       ?>
                                </td>

                                <td style="width:5%;">
                                    <?php
                                    if ($row["proxy_sta"] == 1) {
                                        echo '<span class="label label-success">账号正常</span>';
                                    } else {
                                        echo '<span class="label label-danger">封禁状态</span>';
                                    }
                                    ?>
                                </td>
                                <td style="width:10%;">
                                      <?php echo $row["state_ban"];?>
                                </td>
                                <td style="width:5%;">
                                    <?php
                                    if ($row["proxy_rename_sta"] == 1) {
                                        echo '<span class="label label-info">已实名</span>';
                                    } else {
                                        echo '<span class="label label-info">未实名</span>';
                                    }
                                    ?>
                                </td>
                                <td style="width:5%;">
                                    <?php
                                    if ($row["proxy_type"] == 0) {
                                        echo '<span class="label label-info">个人</span>';
                                    } else {
                                        echo '<span class="label label-info">企业</span>';
                                    }
                                    ?>
                                </td>
                                
                                <td style="width:7%;">
                                    <?php echo $date = date('Y-m-d H:i:s', $row["cr_time"]); ?>
                                </td>
                                
                                <td style="width:15%;">
                                <?php if($row["proxy_sta"] == 1) {?>
                                    <button type="button" class="btn btn-danger btn-sm ban-btn" data-toggle="modal" data-target="#banModal" data-proxyid="<?php echo $row["proxy_id"] ?>" data-whatever="@mdo">封禁账号</button>
                                <?php } else { ?>
                                    <button type="button" class="btn btn-success btn-sm ban-btn" data-toggle="modal" data-target="#banModal" data-proxyid="<?php echo $row["proxy_id"] ?>" data-whatever="@mdo">解封账号</button>
                                <?php } ?>
                                <a href="dwproxy.php?proxy_id=<?php echo $row["proxy_id"] ?>" class="btn btn-warning btn-sm">下级代理</a>
                                <button type="button" class="btn btn-success btn-sm yue-btn" data-toggle="modal" data-target="#yueModal" data-proxyid="<?php echo $row["proxy_id"] ?>" data-whatever="@mdo">余额变更</button>
                                 </td>
                            </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                        <tfoot>
                          <tr>
                            <td colspan="12" style="text-align: right;">
                          <?php
                          echo "<p>共 $maxpage 页&nbsp;&nbsp;";
                          echo "每页 $page_size 项&nbsp;&nbsp;";
                          //设置上一页
                          if ($page > 1) {
                            $prepage = $page - 1;
                            echo "<a href='?curpage=$prepage'>上一页</a>&nbsp;&nbsp;";
                          }
                          //设置下一页
                          if ($page < $maxpage) {
                            $nextpage = $page + 1;
                            echo "<a href='?curpage=$nextpage'>下一页</a>&nbsp;&nbsp;";
                          }
                          echo "&nbsp;&nbsp;第 $page 页</p>";
                        }
                      }
                          ?>
                            </td>
                          </tr>
                        </tfoot>
                  </table>
                </form>
                
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </div>
  </main>
                             <!--这是提交封号-->
                             <div class="modal fade" id="banModal" tabindex="-1" role="dialog" aria-labelledby="banModalLabel">
                              <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                  <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="banModalLabel">封号/解号</h4>
                                  </div>
                                  <div class="modal-body">
                                    <form action="controller/Doproxy_sta.php" method="POST">
                                      <div class="form-group">
                                        <label for="message-text" class="control-label">封禁原因：</label>
                                        <textarea class="form-control" id="message-text" name="state_ban" placeholder="解封账号时，可为空，封号时请输入原因，代理在登录是会提示！" ></textarea>
                                      </div>
                                      <!--id-->
                                      <input type="hidden" name="proxy_id" value="<?php echo $proxy_id ?>">
                                    
                                  </div>
                                  <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary">确认提交</button>
                                  </div>
                                  </form>
                                </div>
                              </div>
                            </div>
                            <!--这里结束封号了-->
                             <!--这是变更余额-->
                             <div class="modal fade" id="yueModal" tabindex="-1" role="dialog" aria-labelledby="yueModalLabel">
                              <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                  <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="yueModalLabel">加额/减额</h4>
                                  </div>
                                  <div class="modal-body">
                                    <form action="controller/proxyyue.php" method="POST">
                                         <div class="form-group">
                                        <label for="price" class="control-label">订单号：</label>
                                        <input type="text" class="form-control" id="oderid" name="oderid"  placeholder="如果需要变更某个订单的金额，则填写此处，如果，没有则为空！" >
                                      </div>
                                        <div class="form-group">
                                        <label for="price" class="control-label">变更金额：</label>
                                        <input type="text" class="form-control" id="price" name="price" placeholder="填写了金额后，选择扣费是扣掉代理的余额，返佣是加余额，不可撤回！">
                                      </div>
                                        <div class="form-group">
                                        <label for="type">金额类型</label>
                                        <div >
                                          <select class="form-control" id="type" name="type" size="1">
                                            <option value="0">返佣</option>
                                            <option value="1">扣费</option>
                                          </select>
                                        </div>
                                      </div>
                                      <!-- <div class="form-group">-->
                                      <!--  <label for="type_model">操作模式<p style="color:red">请注意，月月返需要准确填写订单号且需要所属代理，否则可能会导致数据不生产！</p></label>-->
                                      <!--  <div >-->
                                      <!--    <select class="form-control" id="type_model" name="type_model" size="1">-->
                                      <!--      <option value="0">普通变更金额</option>-->
                                      <!--      <option value="1">月月返现</option>-->
                                      <!--    </select>-->
                                      <!--  </div>-->
                                      <!--</div>-->
                                      <div class="form-group">
                                        <label for="message-text" class="control-label">金额说明：</label>
                                        <textarea class="form-control" id="message-text" name="notes" placeholder="列如某产品月月返佣，则填写订单号KJKJ2023……，平台手动结算！" ></textarea>
                                      </div>
                                      <!--id-->
                                      <input type="hidden" name="proxy_id" value="<?php echo $proxy_id ?>">
                                    
                                  </div>
                                  <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary">确认提交</button>
                                  </div>
                                  </form>
                                </div>
                              </div>
                            </div>
                            <!--这里变更结束了-->
                 <script src="../merchant/assets/js/jquery.min.js"></script>
                <script>
                  $(document).ready(function() {
                    $('.dropdown-menu a').click(function(e) {
                      e.preventDefault();
                      var field = $(this).data('field');
                      var buttonText = $(this).text();
                      $('#search').val(field);
                      $('#search-btn').html(buttonText + ' <span class="caret"></span>');
                    });
                  });
                  
                  // 监听封禁账号按钮的点击事件
                $('.ban-btn').click(function() {
                  var proxyId = $(this).data('proxyid');
                  // 将proxyId设置到模态框中的input元素中
                  $('#banModal input[name="proxy_id"]').val(proxyId);
                });
                
                // 监听余额变更按钮的点击事件
                $('.yue-btn').click(function() {
                  var proxyId = $(this).data('proxyid');
                  // 将proxyId设置到模态框中的input元素中
                  $('#yueModal input[name="proxy_id"]').val(proxyId);
                });
                </script> 
               
  <!--End 页面主要内容-->
  <?php require_once('common/footer.php'); ?>
<?php
} else {
  echo "<script>alert('您尚未登录，没有权限访问该页面');location.href='login.php';</script>";
}
